﻿using UnityEngine;
using System.Collections;

public class DamageFont : MonoBehaviour {

	public static GameObject effectFontRes_heal;
	public static GameObject effectFontRes_damage_player;
	public static GameObject effectFontRes_damage_normal_enemy;
	public static GameObject effectFontRes_damage_skill_enemy;
	static Transform parentHudRoot;
	MassiveBattle.Controller.BaseController me;
	MassiveBattle.Controller.BaseController attackerSource;
	
	bool [] isCreatedHUDText = new bool[7];
	HUDText [] mText = new HUDText[7];
	public GameObject [] hudTextGo = new GameObject[7];
	GameObject [] dummyGo = new GameObject[7];

	public void Clear()
	{
		for(int i=0; i<hudTextGo.Length; i++) {
			Destroy(hudTextGo[i]);
			Destroy(dummyGo[i]);
		}
	}

	public void ShowDamage(bool isHeal, bool isSkill, Transform pointEffectFont, int point, MassiveBattle.Controller.BaseController _me, MassiveBattle.Controller.BaseController source, int _nIndex)
	{
		if (!MassiveBattle.Manager.BaseManager.Instance.UI.IsActive)
			return;

		me = _me;
		attackerSource = source;
		if(parentHudRoot==null)
			parentHudRoot = GameObject.Find("UI Root/MassiveBattleUI").transform;
		if(isHeal)
		{
			if(effectFontRes_heal==null)
				effectFontRes_heal = parentHudRoot.FindChild("LabelHeal").gameObject;
			if (effectFontRes_heal != null)
				CreateFont(0, effectFontRes_heal, pointEffectFont, point);

		}
		else
		{
			GameObject effectFontRes = null;
			if (me.IsSidePlayer) {
				if(effectFontRes_damage_player==null)
					effectFontRes_damage_player = parentHudRoot.FindChild("LabelPlayerDamage").gameObject;
				effectFontRes = effectFontRes_damage_player;
			}
			else {
				if (isSkill)
				{
					if (effectFontRes_damage_skill_enemy == null)
						effectFontRes_damage_skill_enemy = parentHudRoot.FindChild("LabelEnemySkillDamage").gameObject;
					effectFontRes = effectFontRes_damage_skill_enemy;
				}
				else
				{
					if (effectFontRes_damage_normal_enemy == null)
						effectFontRes_damage_normal_enemy = parentHudRoot.FindChild("LabelEnemyDamage").gameObject;
					effectFontRes = effectFontRes_damage_normal_enemy;
				}
			}
			int nIndex = 1;
			if(_nIndex==0) {
				nIndex = Random.Range(1,4);
				if(isSkill)
					nIndex += 3;
			}
			else
				nIndex=_nIndex;
//			if(mText[nIndex] != null && mText[nIndex].GetListCount() >= 5)
//			{
//				int nDelta = Random.Range(1,3);
//				nIndex += nDelta;
//				if(nIndex==4)
//					nIndex = 1;
//				if(nIndex==5)
//					nIndex = 1;
//			}
			if (effectFontRes != null)
				CreateFont(nIndex, effectFontRes, pointEffectFont, point); 
		}
	}

//	int CheckGoodPosition()
//	{
//		int nLeastIndex = 1;
//		int leastCount=0;
//		for(int i=1; i<4; i++) {
//			if(mText[i]==null)
//				return i;
//			else {
//				if(leastCount > mText[i].GetListCount()) {
//					leastCount = mText[i].GetListCount();
//					nLeastIndex=i;
//				}
//			}
//		}
//		return nLeastIndex;
//	}

	Color fontColor = Color.white;
	void CreateFont(int index, GameObject prefab, Transform pointEffectFont, int point)
	{
		if(me is MassiveBattle.Controller.Boss.BaseBossController) {
			if(index>=1 && index<=3) {
				if(mText[index]!=null && mText[index].GetListCount()>=8) {
					if(index==1 )
						index=2;
					else if(index==2)
						index=3;
					else if(index==3)
						index=1;
				}
			}
			else if(index>=4 && index<=6) {
				if(mText[index]!=null && mText[index].GetListCount()>=5) {
					if(index==4 )
						index=5;
					else if(index==5)
						index=6;
					else if(index==6)
						index=4;
				}
			}
		}

		if(!isCreatedHUDText[index]) {
			GameObject child = NGUITools.AddChild(parentHudRoot.gameObject, prefab);
			hudTextGo[index] = child;
			child.SetActive(true);
			//child.transform.localScale = Vector3.one * 0.5f;
			mText[index] = child.GetComponentInChildren<HUDText>();
			// Make the UI follow the target
			if(me is MassiveBattle.Controller.Boss.BaseBossController) {

				UIFollowTarget ftarget = child.AddComponent<UIFollowTarget>();
				mText[index].fTarget = ftarget;
				ftarget.target = pointEffectFont;

			}
			else {
				mText[index].depthOffset = 10;
				UIFollowTarget ftarget = child.AddComponent<UIFollowTarget>();
				mText[index].fTarget = ftarget;
				ftarget.target = pointEffectFont;
			}
			isCreatedHUDText[index]=true;
		}

		if(me is MassiveBattle.Controller.Boss.BaseBossController)
			mText[index].AddString(point.ToString(), fontColor, 0f, Random.Range(-150,150) , Random.Range(-50,50));
		else
			mText[index].AddString(point.ToString(), fontColor, 0f, Random.Range(-20,20),0);
	}

	public void DeleteAllDamageFont()
	{
		for(int i=0; i<7; i++) {
			if(mText[i]!=null)
				mText[i].DeleteAllEntry();
		}
	}

}
